<div id="panel-button" class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				<div class="col-sm-4 pull-left text-left">
					<strong class="customer_name">{{$customer->name}}</strong>
				</div>
				<div class="col-sm-4 pull-right text-right">
					{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
					@if(isset($order->id))
					{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $order->id))}}
					@endif
					{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
				</div>
			</div>
		</div>
	</div>
</div>

<div id="panel-button-fixed" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				<div class="col-sm-4 pull-left text-left">
					<strong class="customer_name">{{$customer->name}}</strong>
				</div>
				<div class="col-sm-4 pull-right text-right">
					{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
					@if(isset($order->id))
					{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $order->id))}}
					@endif
					{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
				</div>
			</div>
		</div>
	</div>
</div>

<div id="panel-error" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div id="error-content" class="panel-body"></div>
		</div>
	</div>
</div>

<div class='row'>
	<div class='col-md-12'>
		<div class="panel panel-default">
			<!--<header class="panel-heading">{{ trans('customer.create') }}</header>-->
			<div class="panel-body">
				{{Form::open(array('method' => 'post', 'class' => 'form-horizontal'))}}
					<table class="table table-striped footable" data-filter="#filter" data-page-size="10" data-filter-minimum="1">
						<thead>
							<tr>
								<th>{{trans('product.name')}}</th>
								<th data-type="numeric">{{trans('product.net')}}</th>
								<th>{{trans('product.brand')}}</th>
								<!--<th>{{trans('product.type')}}</th>-->
								<th style="width: 150px;">{{trans('product.price')}}</th>
								<th style="width: 100px;">{{trans('product.quantity')}}</th>
								<th style="width: 150px;">{{trans('product.total')}}</th>
							</tr>
						</thead>
						<tbody>
							@foreach($products as $row)
							<tr id="product" data-id="{{$row->id}}">
								<td>{{$row->name}}</td>
								<td>{{$row->net}}</td>
								<td>{{$row->brand}}</td>
								<!--<td>{{$row->type}}</td>-->
								<td>{{Form::text('price', $row->price, array('class' => 'form-control', 'data-id' => $row->id, 'id' => 'price', 'data-product' => $row->product))}}</td>
								<td>{{Form::text('quantity', 0, array('class' => 'form-control', 'id' => 'quantity', 'data-id' => $row->id, 'data-product' => $row->product))}}</td>
								<td>{{Form::text('product_total', 0, array('class' => 'form-control', 'id' => 'product_total', 'data-id' => $row->id, 'data-product' => $row->product, 'readonly' => 'readonly'))}}</td>
							</tr>
							@endforeach
						</tbody>
						<tfoot>
							<tr>
								<td colspan="6">
									<ul class="pagination pagination-sm"></ul>
								</td>
							</tr>
						</tfoot>
					</table>

					{{Form::hidden('id', (isset($customer->id))? $customer->id : 0, array('id' => 'id'))}}
					{{Form::hidden('type', $customer->type, array('id' => 'type'))}}
				{{Form::close()}}
			</div>
		</div>
	</div>
</div>

<div id="panel-order-total-fixed" class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="col-sm-12 form-group pull-right">
					{{Form::label('date', trans('order.date'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('date', date("d-m-Y"), array('class' => 'form-control', 'id' => 'date'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('subtotal', trans('order.subtotal'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('subtotal', 0, array('class' => 'form-control', 'id' => 'subtotal', 'readonly' => 'readonly'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('discount', trans('order.discount'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('discount', (isset($customer->discount))? $customer->discount : $discount, array('class' => 'form-control', 'id' => 'discount'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('total', trans('order.total'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('total', 0, array('class' => 'form-control', 'id' => 'total', 'readonly' => 'readonly'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('dept', trans('order.dept'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('dept', 0, array('class' => 'form-control', 'id' => 'dept', 'readonly' => 'readonly'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('totalpay', trans('order.totalpay'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('totalpay', 0, array('class' => 'form-control', 'id' => 'totalpay'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('refund', trans('order.refund'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('refund', 0, array('class' => 'form-control', 'id' => 'refund'))}}
					</div>
				</div>

				<div class="col-sm-12 form-group pull-right">
					{{Form::label('remaining', trans('order.remaining'), array('class' => 'col-sm-9 control-label text-right'))}}
					<div class="col-sm-3 pull-right">
						{{Form::text('remaining', 0, array('class' => 'form-control', 'id' => 'remaining', 'readonly' => 'readonly'))}}
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<div id="panel-order-total" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				<div class="col-sm-4 pull-left text-left">
					<strong class="customer_name">{{$customer->name}}</strong>
				</div>
				<div class="col-sm-4 pull-right text-right">
					{{Form::button(trans('form.save'), array('class' => 'btn btn-primary', 'id' => 'submit'))}}
					@if(isset($customer->id))
					{{Form::button(trans('form.delete'), array('class' => 'btn btn-danger', 'id' => 'delete', 'data-id' => $customer->id))}}
					@endif
					{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
				</div>
			</div>
		</div>
	</div>
</div>

<script>
$(document).ready(function(){
	@if(isset($order->id))
		@foreach($items as $item)
			$("input#quantity[data-id='{{$item->id}}']").val({{$item->quantity}});
			productTotal({{$item->id}});
		@endforeach

		$("input#discount").val({{$order->discount}});
		subtotal();
		getDept();
		remaining();
	@endif


	function getDept(){
		var date = $("input#date").val();
		var customer = {{$customer->id}};

		$.ajax({
			url: '{{URL::to('admin/order/dept')}}',
			type: 'get',
			data: {'date': date, 'customer': customer},
			beforeSend: ajaxLoading(),
			success: function(result){
				var rs = $.parseJSON(result);

				if(rs.error == false){
					var dept = rs.dept;

					$("input#dept").val(dept);
				}else{
					$("input#dept").val(0);
				}

				ajaxLoadSuccess();
			}
		});
	}

	$("input#date").change(function(event) {
		/* Act on the event */
		getDept();
		$("input#totalpay").focus();
	});

	getDept();

	$("#submit").click(function(){
		remaining();

		var subtotal = $("input#subtotal").val();
		var discount = $("input#discount").val();
		var total    = $("input#total").val();
		var totalpay = $("input#totalpay").val();
		var refund   = $("input#refund").val();
		var date     = $("input#date").val();
		
		subtotal     = parseFloat(subtotal);
		discount     = parseInt(discount);
		total        = parseFloat(total);
		totalpay     = parseFloat(totalpay);
		refund       = parseFloat(refund);

		var order = new Array();
		var payment = new Array();
		var items = new Array();

		order = {"customer": {{$customer->id}}, "subtotal": subtotal, "total": total, "discount": discount, "date": date, 'type': '{{$customer->type}}'};
		
		payment = [{"customer": {{$customer->id}}, "type": "payment", "total": totalpay, "date": date}, {"customer": {{$customer->id}}, "type": "refund", "total": refund, "date": date}];

		var i = 0;

		$("input#quantity").each(function(index, el) {
			var id       = $(this).attr('data-id');
			var product  = $(this).attr('data-product');
			var quantity = $(this).val();
			var price    = $("input#price[data-id='" + id + "']").val();

			quantity = parseInt(quantity);
			product  = parseInt(product);
			price    = parseFloat(price);

			if(quantity > 0){
				items[i] = {"product": product, "price": price, "quantity": quantity};
				i++;
			}else{
				$("tr#product[data-id='" + id + "']").remove();
			}
		});

		$.ajax({
			url: '{{URL::to('admin/order/create')}}',
			type: 'post',
			data: {"order": order, "payment": payment, "items": items},
			success: function(result){
				var rs = $.parseJSON(result);

				if(rs.error == false){
					var url = "{{URL::to('admin/order/list/' . $customer->type)}}";
					window.location = url;
				}else{
					appendErrorMessages(rs.message);
					//alert(result);
				}

				ajaxLoadSuccess();
			}
		});
	});

	$("input#quantity").change(function(event) {
		/* Act on the event */
		var id = $(this).attr('data-id');
		var quantity = $(this).val();

		productTotal(id);
	});

	$("input#price").change(function(event) {
		/* Act on the event */
		var price = $(this).val();
		var id = $(this).attr('data-id');

		 deleteErrorMessages();

		$.ajax({
			url: '{{URL::to('admin/product-customer/change-price')}}',
			type: 'post',
			data: {'id': id, 'price': price},
			beforeSend: ajaxLoading(),
			success: function(result){
				var rs = $.parseJSON(result);

				if(rs.error == true){
					appendErrorMessages(rs.message);
				}

				productTotal(id);

				ajaxLoadSuccess();
			}
		});
	});

	function productTotal(id){
		var price = $("input#price[data-id='" + id + "']").val();
		var quantity = $("input#quantity[data-id='" + id + "']").val();
		var total = price * quantity;

		total = parseFloat(total).toFixed(3);
		total = parseFloat(total);

		$("input#product_total[data-id='" + id + "']").val(total);
		subtotal();
		remaining();
	}

	function subtotal(){
		var subtotal = 0;

		$("input#product_total").each(function(index, el) {
			var price = $(this).val();
			price = parseFloat(price);

			subtotal += price;
		});

		subtotal = parseFloat(subtotal).toFixed(3);
		subtotal = parseFloat(subtotal);

		$("input#subtotal").val(subtotal);
		total();
		remaining();
	}

	function total(){
		var discount = $("input#discount").val();
		var subtotal = $("input#subtotal").val();
		var total = (1 - (discount / 100)) * subtotal;

		total = parseFloat(total).toFixed(3);
		total = parseFloat(total);

		$("input#total").val(total);
		remaining();
	}

	$("input#discount, input#totalpay, input#refund, input#quantity").change(function(event) {
		var val = $(this).val();

		if(val == null || isNaN(val) || val < 0 || val.length == 0){
			$(this).val(0);
		}

		remaining();
	});

	$("input#discount").change(function(event) {
		/* Act on the event */
		var discount = $(this).val();

		if(discount > 100){
			discount = 100;
		}

		$(this).val(discount);

		total();
		remaining();
	});

	function remaining(){
		var total = $("input#total").val();
		var dept = $("input#dept").val();
		var totalpay = $("input#totalpay").val();
		var refund = $("input#refund").val();

		total = parseFloat(total).toFixed(3);
		total = parseFloat(total);

		dept = parseFloat(dept).toFixed(3);
		dept = parseFloat(dept);

		totalpay = parseFloat(totalpay).toFixed(3);
		totalpay = parseFloat(totalpay);

		refund = parseFloat(refund).toFixed(3);
		refund = parseFloat(refund);

		var remaining = total + dept - totalpay - refund;
		remaining = parseFloat(remaining).toFixed(3);
		remaining = parseFloat(remaining);

		$("input#remaining").val(remaining);
	}

	$("button#delete").click(function() {
		var name = $(this).attr('data-name');
		var ok = confirm("{{trans('order.confirm_delete')}}");

		if(ok){
			var id = $(this).attr('data-id');

			$.ajax({
				url: '{{URL::to("admin/order/delete")}}',
				type: 'POST',
				data: {'id': id},
				success: function(result){
					var rs = $.parseJSON(result);

					if(rs.error == false){
						var url = "{{URL::to('admin/order/list/' . $customer->type)}}";
						window.location = url;
					}else{
						appendErrorMessages(rs.message);
					}
				}
			});
		}

		return false;
	});

	$("button#cancel").click(function(){
		var url = "{{URL::to('admin/order/list/' . $customer->type)}}";
		window.location = url;
	});
});
</script>
<style type="text/css">
strong.customer_name{
	text-transform: uppercase;
	font-size: 18pt;
}
</style>